url: str = "/home/xieweig/document"


def test01():
    with open(url + "/k.txt", mode='rb') as k:
        a = k.read(307)
        print(a.decode('gbk'))
        b = k.read()
        print(b.decode('utf-8'))


def test02():
    import yaml
    import os.path as p
    sss = p.abspath(p.dirname(__file__)).split('mathPractise')
    print(sss)
    rp = sss[0]
    with open(rp + 'mathPractise/math.yml') as d:
        m = yaml.load(d)
        print(m['datasource']['password'])
        print(m['datasource']['user-name'])
        for i in m['animals']:
            print(i)


def test03():
    import json
    a = {
        "name": 'zhangsan',
        "age": 10,
        "young": False,
        "money": None,
        "goods": ["glass", "fruit"]
    }

    print(a)
    k = json.dumps(a)
    print(k)
    with open(url + "/p.txt", mode='w+', encoding='utf-8') as p:
        p.write(k)
        p.flush()
        import time
        time.sleep(1)
        p.seek(0)
        b = p.read()
        bb = json.loads(b)
        print(bb)


def k2():
    a = 'nice,hi,g'.split(sep=',')
    # 一般性list容器操作 过滤 映射 聚合
    print(a)
    s = ''
    for i in a:
        if 'i' in i:
            i += '-====-'
            print(i)
            s += i
    print(s)
    # 响应式编程的思路
    from functools import reduce

    k = map(lambda y: y + '-====-',
            filter(lambda x: 'i' in x, a))
    m = reduce(lambda r, j: r + j, k)
    print(m)

    # ctrl space enter 可以快速运行pycharm
    # 数学风格
    # u=[x for x in a if 'i' in x]
    u = [x + '-====-' for x in a if 'i' in x]
    print(reduce(lambda r, j: r + j, u))

    # numpy风格 a = a[i] 融为一体

    import numpy as np
    na = np.asarray([10, 29, 12, 39])
    np.dtype.str
    print(na, type(na), na.shape, na.size)
    for i in na:
        print(i)
    # t=na['i' in na]
    t = na[na > 20]

    t = na[np.where(na > 20)]
    print(t)
    # t=t+'-====-'
    # print(t)


import sys as ss


# from sys import stdin,stdout,stderr
def k3():
    ss.stdout.write("hihihi" + '\n')
    screen = ss.stdout
    for iii in range(3):
        k = ss.stdin.readline()
        if (iii == 1):
            ss.stdout = open('./pra.txt', 'w')
            ss.stdout.write(k + '\n')
            continue
        ss.stdout = screen
        ss.stdout.write(k + '\n')


from io import StringIO


def k4():
    s = StringIO()
    s.write("docker redis")
    s.write("mongodb for you")
    m = s.getvalue()

    m1 = s.tell()
    print(m, m1)
    s.seek(0)
    while True:
        kk = s.readline()
        if not kk:
            break
        print(kk.strip())

    s.close()


from io import BytesIO


def k5():
    b = BytesIO()
    b.write("hello".encode("utf-8"))
    print(b.getvalue())
    b.close()


if __name__ == '__main__':
    k5()
